package com.attendance.sz.local;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteException;

import com.attendance.sz.db.DBHandler;
import com.attendance.sz.db.DBHelper;
import com.attendance.sz.exception.DBException;

public class LocalBaseDao {
	/** The database of the basic operation DBHandler object */
	DBHandler dbHandler;
	
	/**
	 * Every operation before judge whether database database open, if not open, opened first database, to return to DBHandler object
	 * 
	 * @return DBHandler 
	 * @throws SQLException
	 * @see DBHandler#openDatabase()
	 */
	DBHandler init() throws DBException {
		if(DBConstants.dbHandler == null)
		{
			DBHelper dbHelper = new DBHelperImpl(DBConstants.DATABASE_NAME, DBConstants.DATABASE_VERSION, DBConstants.DB_CREATE_SQL);
			DBConstants.dbHandler = new DBHandler(dbHelper);
		}
		this.dbHandler = DBConstants.dbHandler;
		if (!dbHandler.isOpen()) {
			try {
				dbHandler.openDatabase();
			} catch (SQLiteException e) {
				throw new DBException("Database init error!", e);
			}
		}
		return this.dbHandler;
	}

	/**
	 * To database insert data
	 * 
	 * @param tableName
	 * @param columns
	 * @param contents
	 * @param startIndex
	 * @throws DBException
	 */
	void insert(String tableName, String columns[], String[] contents,
			int startIndex) throws DBException {
		ContentValues contentValues = buildContentValues(columns, startIndex,
				contents);
		long temp = dbHandler.insert(tableName, contentValues);
		if (-1 == temp) {
			throw new DBException(getClass().getSimpleName()
					+ ": insert error ");
		}
	}

	/**
	 * To update the data in the data, updated returning true, otherwise returns false
	 * 
	 * @param userIdIndex
	 * @param tableName
	 * @param columns
	 * @param startIndex
	 * @param contents
	 * @return boolean 
	 * @throws DBException
	 */
	boolean update(int userIdIndex, String tableName, String columns[],
			int startIndex, String[] contents,String userid) throws DBException {
		ContentValues contentValues = buildContentValues(columns, startIndex,
				contents);
		String whereClause = columns[userIdIndex] + "=?";
		String[] whereArgs = { userid };
		int temp = dbHandler.update(tableName, contentValues, whereClause,
				whereArgs);
		if (temp >= 1) { 
			return true;
		} else {
			return false;
		}
	}


	/**
	 * Delete this UserId record
	 * 
	 * @param tableName
	 * @param columnNameUserId
	 * @throws DBException
	 */
	void delete(String tableName, String columnNameUserId,String userid) throws DBException {
		String whereClause = columnNameUserId + "=?";
		String whereArgs[] = { userid };
		dbHandler.delete(tableName, whereClause, whereArgs);
	}
	
	/**
	 * Lookup < code > tableName < / code > in < code > userId < / code > record
	 * 
	 * @param tableName
	 * @param columnNameUserId
	 * @return 
	 */
	Cursor query(String tableName, String columnNameUserId,String userid ) {
		String querySQL = "select * from " + tableName + " where "
				+ columnNameUserId + " = ?";
		String[] selectionArgs = { userid };
		Cursor cursor = dbHandler.query(querySQL, selectionArgs);
		return cursor;
	}

	/**
	 * Lookup < code > tableName < / code > in < code > userId < / code > record
	 * 
	 * @param tableName
	 * @param columnNameUserId
	 * @param orderBy
	 *          "ORDER BY order ASC"
	 * @return cursor
	 */
	Cursor query(String tableName, String columnNameUserId, String orderBy ,String userid ) {
		String querySQL = "select * from " + tableName + " where "
				+ columnNameUserId + " = ? " + orderBy;
		String[] selectionArgs = { userid };
		Cursor cursor = dbHandler.query(querySQL, selectionArgs);
		return cursor;
	}

	/**
	 * Lookup < code > tableName < / code > in < code > userId < / code > record
	 * 
	 * @param tableName
	 * @param columnNameUserId
	 * @param orderBy
	 *           "ORDER BY order ASC"
	 * @return cursor
	 */
	Cursor query(String tableName) {
		String querySQL = "select * from " + tableName;
		Cursor cursor = dbHandler.query(querySQL);
		return cursor;
	}

	/**
	 * Lookup < code > tableName < / code > in < code > userId < / code > record
	 * 
	 * @param tableName
	 * @param columnNameUserId
	 * @param orderBy
	 *          "ORDER BY order ASC"
	 * @return cursor
	 */
	Cursor queryOrderBy(String tableName,String oderBy) {
		String querySQL = "select * from " + tableName + " "+oderBy;
		Cursor cursor = dbHandler.query(querySQL);
		return cursor;
	}
	
	/**
	 * Lookup < code > tableName < / code > of record
	 * @param tableName 
	 * @param selection where "uid = ?"
	 * @param selectionArgs 
	 * @param orderBy  "ORDER BY order ASC"
	 * @return cursor
	 */
	Cursor query(String tableName, String selection, String[] selectionArgs,
			String orderBy) {
		Cursor cursor = dbHandler.query(tableName, null, selection,
				selectionArgs, orderBy);
		return cursor;
	}

	/**
	 * Generate insert to update the database of the required contentValues (key-value pairs.) 
	 * array columns and contents length should be consistent
	 * @param columns
	 * @param startIndex
	 * @param contents
	 * @return contentValues
	 */
	ContentValues buildContentValues(String columns[], int startIndex,
			String[] contents) {
		if (columns == null || contents == null) {
			throw new IllegalArgumentException("array is null");
		}
		final int N = columns.length;
		if (startIndex < 0 || startIndex >= N || N != contents.length) {
			throw new IllegalArgumentException("array index error.");
		}
		ContentValues contentValues = new ContentValues();
		for (int i = startIndex; i < N; i++) {
			contentValues.put(columns[i], contents[i]);
		}
		return contentValues;
	}
	
	/**
	 * Whether in the table with the current uesr_id and other_id exist at a record
	 */
	boolean haveTheRecord(String table, String id,String value,String columnNameUserId, String userid){
		Cursor cursor = queryRecord(table,id,value,columnNameUserId ,userid);
		boolean isExist = cursor.moveToFirst();
		cursor.close();
		return isExist;
	}
	
	/**
	 * Lookup < code > tableName < / code > in < code > userId < / code > record
	 * 
	 * @param tableName
	 * @param merchant_id
	 * @return cursor
	 */
	Cursor queryRecord(String tableName, String id ,String value ,String columnNameUserId , String userid ) {
		String querySQL = "select * from " + tableName + " where "
				+ id+ " = ?  and " + columnNameUserId +" = ? ";
		String[] selectionArgs = { value, userid };
		Cursor cursor = dbHandler.query(querySQL, selectionArgs);
		return cursor;
	}
	
	/**
	 * To update the data in the data, updated returning true, otherwise returns false
	 * 
	 * @param userIdIndex
	 * @param tableName
	 * @param columns
	 * @param startIndex
	 * @param contents
	 * @return boolean 
	 * @throws DBException
	 */
	boolean update(int userIdIndex, int otherIdIndex, String tableName, String columns[],
			int startIndex, String[] contents, String otherid, String userid ) throws DBException {
		ContentValues contentValues = buildContentValues(columns, startIndex,
				contents);
		String whereClause = columns[userIdIndex] + "=? and " + columns[otherIdIndex] + "=?";
		String[] whereArgs = { userid,otherid};
		int temp = dbHandler.update(tableName, contentValues, whereClause,
				whereArgs);
		if (temp >= 1) { 
			return true;
		} else {
			return false;
		}
	}
}
